//
//  WJCityViewController.m
//  天气
//
//  Created by dllo on 16/8/23.
//  Copyright © 2016年 WJ. All rights reserved.
//

#import "WJCityViewController.h"
#import "WJNavinationView.h"

#define kWJWidth  [UIScreen mainScreen].bounds.size.width
#define kWJHeight [UIScreen mainScreen].bounds.size.height

@interface WJCityViewController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UISearchBar *searchBar;

@property (nonatomic, strong) UITableView *searchTableView;

@property (nonatomic, strong) NSMutableArray *searchArray;

@property (nonatomic, strong) NSMutableArray *cityName;

@end

@implementation WJCityViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.searchArray = [NSMutableArray array];
    
    [self analysisCityData];
    
    [self createConfig];
    
    [self createView];
    
}
- (void)createConfig
{
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    image.image = [UIImage imageNamed:@"blur_bg2.jpg"];
    image.userInteractionEnabled = YES;
    [self.view addSubview:image];
    
    WJNavinationView *view = [[WJNavinationView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(80, 14, kWJWidth - 100, 44)];
    self.searchBar.placeholder = @"搜索您想要关注的";
    self.searchBar.backgroundColor = [UIColor clearColor];
    self.searchBar.translucent = YES;
    [_searchBar setBackgroundImage:[UIImage new]];
    self.searchBar.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.35];
    [view addSubview:self.searchBar];
    self.searchBar.delegate = self;
    
    
    view.Block = ^(){
        [self.navigationController popToRootViewControllerAnimated:YES];
    };
    [self.view addSubview:view];
    
}

- (void)createView
{
    
    self.searchTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 114, kWJWidth, kWJHeight - 114) style:UITableViewStylePlain];
    [self.view addSubview:self.searchTableView];
    self.searchTableView.backgroundColor = [UIColor clearColor];
    self.searchTableView.delegate = self;
    self.searchTableView.dataSource = self;
    self.searchTableView.rowHeight = 30;
    self.searchTableView.backgroundColor = [UIColor clearColor];
    self.searchTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.searchTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"searchTableView"];
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.searchArray.count) {
        return self.searchArray.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchTableView"];
    cell.backgroundColor = [UIColor clearColor];
    
    if (self.searchArray.count) {
        NSDictionary *model =self.searchArray[indexPath.row];
        cell.textLabel.textColor = [UIColor grayColor];
        
        //设置标签文字属性
        NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:model.allKeys[0]];
        NSRange rang = [model.allKeys[0] rangeOfString:self.searchBar.text options:NSCaseInsensitiveSearch];
        [attributeString setAttributes:[NSMutableDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, nil] range:rang];
        
        cell.textLabel.attributedText = attributeString;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.searchArray.count) {
        
        NSDictionary *model =self.searchArray[indexPath.row];
        self.Block(model);
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    //    NSLog(@"%@",searchText);
}
// 显示搜索按钮 并且设置它的状态
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = [UIFont systemFontOfSize:15];
    attr[NSForegroundColorAttributeName] = [UIColor redColor];
    [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:@"搜索"];
    [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:attr forState:UIControlStateNormal];
    [searchBar setShowsCancelButton:YES animated:YES];
    return YES;
}
#pragma mark -  点击搜索按钮 实现的方法
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    if (searchBar.text.length !=0 ) {
        //清空搜索数组
        [self.searchArray removeAllObjects];
        
        for (NSDictionary *model in self.cityName) {
            
            NSString* str = searchBar.text;
            
            // 是否包含那两个字  创建条件@""里面是条件语句
            NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c]%@ ",str];
            
            // 下面的代码就是剩下的代码
            if ([namePredicate evaluateWithObject:model.allKeys[0]]) {
                [self.searchArray addObject:model];
            }
        }
        [self.searchTableView reloadData];
        
    }
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    if (searchBar.text.length !=0 ) {
        //清空搜索数组
        [self.searchArray removeAllObjects];
        
        for (NSDictionary *model in self.cityName) {
            
            NSString* str = searchBar.text;
            
            // 是否包含那两个字  创建条件@""里面是条件语句
            NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c]%@ ",str];
            
            // 下面的代码就是剩下的代码
            if ([namePredicate evaluateWithObject:model.allKeys[0]]) {
                [self.searchArray addObject:model];
            }
        }
        [self.searchTableView reloadData];
        
    }
    
    
}


- (void)analysisCityData
{
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"weatherPlist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSArray *array = dic[@"zone"];
    
    // 根据编码
    // 数组套字典 cityArray {"市名": "北京", "编码": "101010100"}
    NSMutableArray *cityArray = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        
        NSArray *array = dic[@"zone"];
        for (NSDictionary *dic in array) {
            [cityArray addObject:dic[@"zone"]];
        }
        
    }
    
    // 数组套字典 cityArray1  {"北京": "101010100"}
    NSMutableArray *array1 = [NSMutableArray array];
    for (NSArray *array in cityArray) {
        for (NSDictionary *dic in array) {
            
            NSString *cityName = dic[@"name"];
            NSString *code = dic[@"code"];
            NSDictionary *cityDic =[ NSDictionary  dictionaryWithObjectsAndKeys:code,cityName, nil];
            [array1 addObject:cityDic];
            
        }
    }
    
    self.cityName = array1;
}



























@end
